package com.abigdreamer.newspider;
/**   
 * 
 * @author Darkness
 * @date 2013-6-2 下午04:38:25
 * @website http://www.abigdreamer.com 
 * @version V1.0   
 */
public abstract class ThreadTaskProcesser<T extends ThreadTaskManager> implements Runnable {
	
	public boolean isRuning = false;
	
	private T threadTaskManager;
	
	public ThreadTaskProcesser(T threadTaskManager) {
		this.threadTaskManager = threadTaskManager;
	}
	
	protected T getThreadTaskManager() {
		return threadTaskManager;
	}
	
	@Override
	public void run() {
		Object tmp = null;
		while ((tmp = threadTaskManager.getTask()) != null || !threadTaskManager.isFinished()) { // 当还有记录时就处理
			if (tmp != null && !"".equals(tmp)) {
				isRuning = true;
				// 处理将一条信息保存到数据库
				
				doTask(tmp);
				
				isRuning = false;
			} else {// 如果没标志处理则休眠一秒再重新开始处理
				isRuning = false;
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	protected abstract void doTask(Object tmp) ;
}
